<template>
  <div class="app-container">
    <el-form
      size="default"
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item class="mr" label="设备编号" prop="equipNo">
        <el-input
          v-model="queryParams.equipNo"
          placeholder="请输入"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="区域名称" prop="equipRegion">
        <el-select
          v-model="queryParams.equipRegion"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="{ label, value } in zoneOptions"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="状态" prop="equipStatus">
        <el-select
          v-model="queryParams.equipStatus"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="{ label, value } in sey_equip_status"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 50px">
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          v-hasPermi="['gallery:records:query']"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['alarm:records:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['alarm:records:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['alarm:records:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['alarm:records:export']"
          >导出</el-button
        >
      </el-col>
    </el-row> -->

    <div class="card-list">
      <el-row justify="space-around">
        <el-col :span="4">
          <div class="card-item">
            <div class="card-item-title">设备总数</div>
            <div class="card-item-content">
              {{ statistics.equipTotal ?? 0 }}
            </div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="card-item">
            <div class="card-item-title">在线设备</div>
            <div class="card-item-content">
              {{ statistics.onlineEquip ?? 0 }}
            </div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="card-item">
            <div class="card-item-title">磁盘使用</div>
            <div class="card-item-content">{{ statistics.diskUsage ?? 0 }}</div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="card-item">
            <div class="card-item-title">离线设备</div>
            <div class="card-item-content">
              {{ statistics.offLineEquip ?? 0 }}
            </div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="card-item">
            <div class="card-item-title">故障设备</div>
            <div class="card-item-content">{{ statistics.faultEquip }}</div>
          </div>
        </el-col>
      </el-row>
    </div>

    <el-table
      v-loading="loading"
      :data="recordsList"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column label="序号" width="80" align="center" type="index" />
      <el-table-column label="设备编号" align="center" prop="equipNo">
        <template #default="{ row }">
          {{ 'No.' + row.equipNo }}
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="equipType">
        <template #default="{ row }">
          <dict-tag :options="sey_equip_type" :value="row.equipType" />
        </template>
      </el-table-column>
      <el-table-column label="设备位置" align="center" prop="equipPosition" />
      <el-table-column label="所在区域" align="center" prop="equipRegionText" />
      <el-table-column label="运行状态" align="center" prop="equipStatus /">
        <template #default="{ row }">
          <dict-tag :options="sey_equip_status" :value="row.equipStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="text"
            icon="View"
            @click="handleView(scope.row)"
            v-hasPermi="['gallery:records:detail']"
            >详细
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 录像详细 -->
    <el-dialog title="录像详细" v-model="openView" width="500px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="设备编号：">{{
              'No.' + form.equipNo
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备类型：">
              <dict-tag :options="sey_equip_type" :value="form.equipType" />
            </el-form-item>
            <el-form-item label="设备位置：">
              {{ form.equipPosition }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在区域：">
              {{ form.equipRegionText }}
            </el-form-item>
            <el-form-item label="运行状态：">
              <dict-tag :options="sey_equip_status" :value="form.equipStatus" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openView = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="monitorRecords">
import {
  getStatistics,
  listRecords,
  addRecords,
  updateRecords,
  delRecords,
  getRecords
} from '@/api/security/monitor/records'

import { zoneTreeSelect } from '@/api/emergency/mats'

const { proxy } = getCurrentInstance()

/* 获取单位字典 */
const { sey_equip_status } = proxy.useDict('sey_equip_status') //设备状态
const { sey_equip_type } = proxy.useDict('sey_equip_type') //设备类型

const statistics = ref([])
const recordsList = ref([])
const open = ref(false)
const loading = ref(true)
const sns = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const zoneOptions = ref([])
const openView = ref(false)
const title = ref('')
const total = ref(0)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  }
})

const { queryParams, form } = toRefs(data)

/** 存放位置树结构 */
function getZoneTree() {
  return zoneTreeSelect().then((response) => {
    zoneOptions.value = response.data
  })
}

/** 查询统计数据 */
function getData() {
  getStatistics().then((response) => {
    statistics.value = response.data
  })
}

/** 查询通风口列表 */
function getList() {
  loading.value = true
  listRecords(queryParams.value).then((response) => {
    recordsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

/* 重置新增和修改表单 */
function reset() {
  form.value = {}
  proxy.resetForm('recordsRef')
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加通风口信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const recordsId = row.id || ids.value
  getRecords(recordsId).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改通风口信息'
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const recordsIds = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除序号为："' + sns.value + '"的数据项?')
    .then(function () {
      return delRecords(recordsIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'pipemanage/records/export',
    {
      ...queryParams.value
    },
    `records_${new Date().getTime()}.xlsx`
  )
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['recordsRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateRecords(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addRecords(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 把每一行的索引放进row */
function tableRowClassName({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  sns.value = selection.map((item) => item.index).sort()
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 详细按钮操作 */
function handleView(row) {
  openView.value = true
  form.value = row
}
// 获取最新搜索下拉选项
getZoneTree()
// 初始化统计
getData()
// 初始化列表
getList()
</script>
<style lang="scss" scoped>
.mr {
  margin-right: 5px;
}
.card-item {
  border: #dcdfe6 1px solid;
  padding: 30px 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 12px;
  text-align: center;
  .card-item-title {
    font-size: 18px;
    color: #606266;
    line-height: 1;
    margin-bottom: 20px;
  }
  .card-item-content {
    color: #e6a23c;
  }
}
</style>
